package com.ruoyi.project.hidden.hiddenLink.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.project.hidden.hiddenName.domain.HiddenName;
import com.ruoyi.project.hidden.hiddenName.service.IHiddenNameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.hidden.hiddenLink.domain.HiddenLink;
import com.ruoyi.project.hidden.hiddenLink.service.IHiddenLinkService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 隐患环节Controller
 * 
 * @author ruoyi
 * @date 2021-01-21
 */
@Controller
@RequestMapping("/hidden/hiddenLink")
public class HiddenLinkController extends BaseController
{
    private String prefix = "hidden/hiddenLink";

    @Autowired
    private IHiddenLinkService hiddenLinkService;
    @Autowired
    private IHiddenNameService hiddenNameService;

    @RequiresPermissions("hidden:hiddenLink:view")
    @GetMapping()
    public String hiddenLink()
    {
        return prefix + "/hiddenLink";
    }

    /**
     * 查询隐患环节列表
     */
    @RequiresPermissions("hidden:hiddenLink:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HiddenLink hiddenLink)
    {
        startPage();
        List<HiddenLink> list = hiddenLinkService.selectHiddenLinkList(hiddenLink);
        return getDataTable(list);
    }

    /**
     * 导出隐患环节列表
     */
    @RequiresPermissions("hidden:hiddenLink:export")
    @Log(title = "隐患环节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HiddenLink hiddenLink)
    {
        List<HiddenLink> list = hiddenLinkService.selectHiddenLinkList(hiddenLink);
        ExcelUtil<HiddenLink> util = new ExcelUtil<HiddenLink>(HiddenLink.class);
        return util.exportExcel(list, "hiddenLink");
    }

    /**
     * 新增隐患环节
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存隐患环节
     */
    @RequiresPermissions("hidden:hiddenLink:add")
    @Log(title = "隐患环节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HiddenLink hiddenLink)
    {
        return toAjax(hiddenLinkService.insertHiddenLink(hiddenLink));
    }

    /**
     * 修改隐患环节
     */
    @GetMapping("/edit/{linkId}")
    public String edit(@PathVariable("linkId") Long linkId, ModelMap mmap)
    {
        HiddenLink hiddenLink = hiddenLinkService.selectHiddenLinkById(linkId);
        mmap.put("hiddenLink", hiddenLink);
        return prefix + "/edit";
    }

    /**
     * 修改保存隐患环节
     */
    @RequiresPermissions("hidden:hiddenLink:edit")
    @Log(title = "隐患环节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HiddenLink hiddenLink)
    {
        return toAjax(hiddenLinkService.updateHiddenLink(hiddenLink));
    }

    /**
     * 删除隐患环节
     */
    @RequiresPermissions("hidden:hiddenLink:remove")
    @Log(title = "隐患环节", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(hiddenLinkService.deleteHiddenLinkByIds(ids));
    }

    @PostMapping( "/checkLinkNameUnique")
    @ResponseBody
    public int checkLinkNameUnique(HiddenLink hiddenLink)
    {
        return hiddenLinkService.checkLinkNameUnique(hiddenLink);
    }



    @PostMapping( "/queryLinkList")
    @ResponseBody
    public List<HiddenLink> queryLinkList(HiddenLink hiddenLink){
        List<HiddenLink> list = hiddenLinkService.selectHiddenLinkList(hiddenLink);
        return  list;
    }

    @PostMapping("/toImport/{sysId}/{linkId}")
    @ResponseBody
    public AjaxResult toImport(MultipartFile file,@PathVariable("sysId")Long sysId, @PathVariable("linkId")Long linkId) throws Exception {
        ExcelUtil<HiddenName> util = new ExcelUtil<HiddenName>(HiddenName.class);
        List<HiddenName> nameList = util.importExcel(file.getInputStream());
        System.out.println(nameList);
        String message = hiddenNameService.importEntity(nameList,sysId,linkId);
        return AjaxResult.success(message);
    }

    @GetMapping("/toDetail/{linkId}")
    public String toDetail(@PathVariable("linkId") Long linkId, ModelMap mmap) {
        HiddenName hiddenName = new HiddenName();
        hiddenName.setLinkId(linkId);
        List<HiddenName> list = hiddenNameService.selectHiddenNameList(hiddenName);
        mmap.put("list", list);
        return prefix + "/detail";
    }

}
